package com.xiaoyu.client.ui.activity.main.mine.wallet_and_member;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.xiaoyu.client.R;
import com.xiaoyu.client.model.mine.WalletParticularsBean;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.adapter.AllPowerfulAdapter;
import com.xiaoyu.commonlib.ui.base.BaseActivityWithTitle;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author jx
 * @date 创建时间 2018/10/12
 * @Description 我的 -  我的钱包 - 明细
 */
public class ParticularsActivity extends BaseActivityWithTitle {

    public static final String BILL_WALLET_ID = "bill_wallet_id";
    @BindView(R.id.common_recycler_view)
    RecyclerView mRecyclerView;
    private AllPowerfulAdapter mAdapter;
    @BindView(R.id.smart_refresh_layout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.common_recycler_view_empty_page)
    ImageView mEmptyImg;
    private List<WalletParticularsBean.DataBean.ListBean> mList = new ArrayList<>();

    @Override
    protected void loadContentView(ViewGroup parent) {
        setTitleText("明细");
        View mView = LayoutInflater.from(this).inflate(R.layout.common_recycler_view, parent, true);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setPadding(0, 16, 0, 0);
        mRecyclerView.setBackgroundColor(getResources().getColor(R.color.color_gray_F3));

        smartRefreshLayout.setEnableLoadMore(false);
        smartRefreshLayout.setEnableRefresh(false);
        getParticulsData();


    }

    //解析数据
    private void getParticulsData() {
        NetworkManager.getParticularsList(new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                succeedResult(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }

    private void succeedResult(String result) {
        WalletParticularsBean.DataBean bean = new Gson().fromJson(result, WalletParticularsBean.DataBean.class);
        if (bean.getList() == null) {
            smartRefreshLayout.setVisibility(View.GONE);
            mEmptyImg.setVisibility(View.VISIBLE);
            mEmptyImg.setImageResource(R.mipmap.no_data);
        } else {
            mList.addAll(bean.getList());
            setAdapter(mList);
        }
    }


    //设置适配器显示数据
    private void setAdapter(List<WalletParticularsBean.DataBean.ListBean> mList) {
        mAdapter = new AllPowerfulAdapter<WalletParticularsBean.DataBean.ListBean>(R.layout.particulars_item, mList, new AllPowerfulAdapter.OnClickListener<WalletParticularsBean.DataBean.ListBean>() {
            @Override
            public void setOnClick(BaseViewHolder baseViewHolder, WalletParticularsBean.DataBean.ListBean particularsBean, int position) {
                Intent intent = new Intent(ParticularsActivity.this, OrderDetailsActivity.class);
                intent.putExtra(BILL_WALLET_ID, particularsBean.getWalletid());//账单ID
                startActivity(intent);
            }
        }) {
            @Override
            protected void convert(BaseViewHolder baseViewHolder, WalletParticularsBean.DataBean.ListBean listBean) {
                super.convert(baseViewHolder, listBean);

                baseViewHolder.setText(R.id.particulars_order_name, listBean.getMoneytype());
                baseViewHolder.setText(R.id.particulars_order_time, listBean.getMoneyaddtime());
                baseViewHolder.setText(R.id.particulars_order_money, listBean.getMoney());
            }
        };

        mRecyclerView.setAdapter(mAdapter);
    }

}
